package com.dinner.model;

import java.sql.Connection;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AdministratorDaoHibernate 	extends 	HibernateDaoSupport 
										implements 	AdministratorDao {

	@Override public Administrator getUserForLogin(String adminID, String password) {
		SessionFactory sessionFactory = this.getSessionFactory();
		Session session = sessionFactory.openSession();
		int isolation = -1;
		try {
			isolation = session.connection().getTransactionIsolation();
			session.connection().setTransactionIsolation(
					Connection.TRANSACTION_SERIALIZABLE);
			session.beginTransaction();
			Administrator admin = (Administrator) getHibernateTemplate().get(
					Administrator.class, adminID);
			session.getTransaction().commit();
			session.connection().setTransactionIsolation(isolation);
			if (admin != null && admin.getPassword().equals(password)) {
				return admin;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			Transaction tx = session.getTransaction();
			if (tx.isActive())
				tx.rollback();
			return null;
		} finally {
			session.close();
		}
	}

}
